#!/bin/python
import pandas as pd
import numpy as np
from time import gmtime, strftime
def now():
    return strftime("%Y-%m-%d %H:%M:%S", gmtime())

print now(), 'start clean'
ratings = pd.read_csv('./raings_binarized.dat',header=None,sep=' ',names=['u','i','r'])
users = (ratings['u'].unique())
users = np.random.permutation(users)
k =int(users.shape[0] * 0.9)
train_user = users[:k]
test_user = users[k:]
print now(), 'spliting train-test'
ratings_train = ratings[ratings['u'].isin(train_user)].copy()
ratings_test = ratings[ratings['u'].isin(test_user)].copy()
ratings_train.to_csv('./ratings_train.dat',header=False,sep= ' ', index = False)
ratings_train = ratings_train[['i','u','r']]
ratings_train.to_csv('./ratings_train_iuv.dat',header=False,sep= ' ', index = False)

test_input = pd.DataFrame()
test_eval = pd.DataFrame()
print now(), 'spliting test data'
for u in test_user:
    transactions = ratings_test[ratings_test['u'] == u]
    n = transactions.shape[0]
    k = int(n/2)
    test_input = test_input.append(transactions.iloc[:k,:])
    test_eval = test_eval.append(transactions.iloc[k:,:])

#    for i in xrange(k):
#        test_input = test_input.append(transactions.iloc[i,:])
#    for j in xrange(k,n):
#        test_eval = test_eval.append(transactions.iloc[j,:])
test_input = test_input[['u','i','r']].astype(int)
test_eval = test_eval[['u','i','r']].astype(int)
test_input.to_csv('./ratings_test_input.dat',header=False,sep= ' ', index = False)
test_eval.to_csv('./ratings_test_eval.dat',header=False,sep= ' ', index = False)
pd.DataFrame(test_input['u'].unique()).to_csv('./target_users.dat',header=False,sep= ' ', index = False)

print now(), 'done'
